import os
import sys

script_path = os.path.abspath(__file__)
script_dir = os.path.dirname(script_path)
sys.path.insert(0, script_dir)
from baseClass.other import *
from baseClass.plant import *
from baseClass.zombie import *
from baseClass.amount import *
from typing import Dict
from threading import Timer


class ordinary_zombie(Zombie):
    def __init__(self, x: int, y: int):
        super().__init__(
            BVolume=Life(300),
            MSpeed=MSpeed(-0.5),
            KSpeed=KSpeed(1),
            Damage=Damage(20),
            KillTarget=Plant,
        )
        self.gifs: Dict[Literal["走路", "啃食", "倒地"], gif_frame] = {}
        self.gifs["啃食"] = gif_frame(
            "../../PVZ素材/僵尸/普通僵尸/普通僵尸啃食.gif", 0.8
        )
        self.gifs["走路"] = gif_frame(
            "../../PVZ素材/僵尸/普通僵尸/普通僵尸走路.gif", 0.8
        )
        self.gifs["倒地"] = gif_frame("../../PVZ素材/僵尸/普通僵尸/僵尸死.gif", 0.8)
        self.gif_frame = self.gifs["走路"]
        self.image = self.gif_frame.action()
        self.rect = self.image.get_rect()
        self.rect.left = x
        self.rect.bottom = y
        self.rect.width = self.rect.width * 0.6
        self.last_update_time = pygame.time.get_ticks()
        self.kill_start = False

    def update(self):
        """更新"""
        # 单个精灵帧率控制
        if pygame.time.get_ticks() - self.last_update_time > 100:
            self.last_update_time = pygame.time.get_ticks()
            if self.status == 1:
                self.rect.x += self.MSpeed
            elif self.status == 2:
                self.gif_frame = self.gifs["啃食"]
            elif self.status == 3:
                if not self.kill_start:
                    self.kill_start = not self.kill_start
                    self.gif_frame = self.gifs["倒地"]
                    self.rect.x -= self.rect.width
                    Timer(1.05, self.kill).start()
            self.image = self.gif_frame.action()
